package redisclient

import (
	"gitee.com/lsy007/redisclient/model"
	"github.com/pquerna/ffjson/ffjson"
)

// HDel(key string, fields ...string) (int64, err error)
// HExists(key string, field string) (bool, err error)
// HGet(key string, field string) (string, err error)
// HGetAll(key string) (map[string]string, err error)
// HIncrBy(key string, field string, incr int64) (int64, err error)
// HIncrByFloat(key string, field string, incr float64) (float64, err error)
// HKeys(key string) ([]string, err error)
// HLen(key string) (int64, err error)
// HMGet(key string, fields ...string) ([]string, err error)
// HMSet(key string, fieldValue ...string) (bool, err error)
// HSet(key string, field string, value string) (int64, err error)
// HSetNX(key string, field string, value string) (bool, err error)
// HVals(key string) ([]string, err error)
// HScan(key string, cursor int64, match string, count int64) ([]string, int64, err error)

// 删除一个或多个哈希表字段
func (c *RedisClient) HDel(key string, fields ...string) (count int64, err error) {
	args := &model.RedisArgsModel{Key: key, Fields: fields}
	response, err = c.Request("HDel", "Hash", args)
	return response.Int, err
}

// 查看哈希表 key 中，指定的字段是否存在
func (c *RedisClient) HExists(key string, field string) (has bool, err error) {
	args := &model.RedisArgsModel{Key: key, Field: field}
	response, err = c.Request("HExists", "Hash", args)
	return response.Bool, err
}

// 获取存储在哈希表中指定字段的值
func (c *RedisClient) HGet(key string, field string) (value string, err error) {
	args := &model.RedisArgsModel{Key: key, Field: field}
	response, err = c.Request("HGet", "Hash", args)
	return response.String, err
}

// 获取在哈希表中指定 key 的所有字段和值
func (c *RedisClient) HGetAll(key string) (fieldValue map[string]string, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("HGetAll", "Hash", args)
	return response.StringStringMap, err
}

// 为哈希表 key 中的指定字段的整数值加上增量 increment
func (c *RedisClient) HIncrBy(key string, field string, incr int64) (value int64, err error) {
	args := &model.RedisArgsModel{Key: key, Field: field, IncrInt: incr}
	response, err = c.Request("HIncrBy", "Hash", args)
	return response.Int, err
}

// 为哈希表 key 中的指定字段的浮点数值加上增量 increment
func (c *RedisClient) HIncrByFloat(key string, field string, incr float64) (value float64, err error) {
	args := &model.RedisArgsModel{Key: key, Field: field, IncrFloat: incr}
	response, err = c.Request("HIncrByFloat", "Hash", args)
	return response.Float, err
}

// 获取哈希表中所有的字段
func (c *RedisClient) HKeys(key string) (fields []string, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("HKeys", "Hash", args)
	return response.StringSlice, err
}

// 获取哈希表中字段的数量
func (c *RedisClient) HLen(key string) (len int64, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("HLen", "Hash", args)
	return response.Int, err
}

// 获取所有给定字段的值
func (c *RedisClient) HMGet(key string, fields ...string) (values []interface{}, err error) {
	args := &model.RedisArgsModel{Key: key, Fields: fields}
	response, err = c.Request("HMGet", "Hash", args)
	values = make([]interface{}, 0)
	if response.Bytes != nil {
		err = ffjson.Unmarshal(response.Bytes, &values)
	}
	return
}

// 同时将多个 field-value (域-值)对设置到哈希表 key 中
func (c *RedisClient) HMSet(key string, fieldValues ...interface{}) (status bool, err error) {
	var bytes []byte
	if bytes, err = ffjson.Marshal(fieldValues); err != nil {
		return
	}
	args := &model.RedisArgsModel{Key: key, Bytes: bytes}
	response, err = c.Request("HMSet", "Hash", args)
	return response.Bool, err
}

// 将哈希表 key 中的字段 field 的值设为 value
func (c *RedisClient) HSet(key string, field string, value string) (len int64, err error) {
	args := &model.RedisArgsModel{Key: key, Field: field, Value: value}
	response, err = c.Request("HSet", "Hash", args)
	return response.Int, err
}

// 只有在字段 field 不存在时，设置哈希表字段的值
func (c *RedisClient) HSetNX(key string, field string, value string) (status bool, err error) {
	args := &model.RedisArgsModel{Key: key, Field: field, Value: value}
	response, err = c.Request("HSetNX", "Hash", args)
	return response.Bool, err
}

// 获取哈希表中所有值
func (c *RedisClient) HVals(key string) (values []string, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("HVals", "Hash", args)
	return response.StringSlice, err
}

// 迭代获取哈希表中的键值对
func (c *RedisClient) HScan(key string, cursor int64, match string, count int64) (fieldValues []string, nextCursor int64, err error) {
	args := &model.RedisArgsModel{Key: key, Cursor: cursor, Match: match, Count: count}
	response, err = c.Request("HScan", "Hash", args)
	return response.StringSlice, response.Int, err
}
